﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace AfterDash
{
    class DashboardData
    {
        AfterDash dashboard;
        byte[] data = new byte[1024];
        string stringData;
        UdpClient server;
        IPEndPoint sender;

        Thread recvThread;

        public DashboardData(AfterDash dash)
        {
            dashboard = dash;
        }

        public void StartDashboard()
        {
            server = new UdpClient(1140);
            sender = new IPEndPoint(IPAddress.Any, 1140);
            recvThread = new Thread(recv);
            recvThread.Start();
        }

        public void StopDashboard()
        {
            recvThread.Abort();
            server.Close();
        }

        void recv()
        {
            while (true)
            {
                data = server.Receive(ref sender);
                stringData = Encoding.ASCII.GetString(data, 0, data.Length);
                dashboard.DisplayData(stringData);
            }
        }
    }
}
